package com.jbm.jbmsupplier.activity;


import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.presenters.AccmPlishActivitys;
import com.jbm.jbmsupplier.utlis.ExitActivity;
import com.jbm.jbmsupplier.utlis.Time_Transformation;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.jbmsupplier.views.AccmPlishViews;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.jbm.jbmsupplier.R.id.layout_quxiaoqueren;

/**
 * Created by 家帮忙 on 2017/4/14.
 */

public class AccomPlishActivity extends AllActivityApp implements AccmPlishViews {
    @InjectView(R.id.teleft)
    TextView teleft;
    @InjectView(R.id.tetitle)
    TextView tetitle;
    @InjectView(R.id.baoxiuqi)
    EditText baoxiuqi;
    @InjectView(R.id.weixiuneirong)
    EditText weixiuneirong;
    @InjectView(R.id.jine)
    EditText jine;
    @InjectView(R.id.tjtp)
    TextView tjtp;
    @InjectView(R.id.paizhao)
    ImageView paizhao;
    @InjectView(R.id.tijiaoneirong)
    Button tijiaoneirong;
    @InjectView(R.id.imdelete)
    ImageView imdelete;
    @InjectView(R.id.fuwuneirong)
    EditText fuwuneirong;
    @InjectView(R.id.cailiaomingxi)
    EditText cailiaomingxi;
    @InjectView(R.id.fuwujine)
    EditText fuwujine;
    @InjectView(R.id.cailiaojine)
    EditText cailiaojine;
    @InjectView(R.id.zongjine)
    EditText zongjine;
    @InjectView(R.id.layout_woaiwojia)
    LinearLayout layoutWoaiwojia;
    @InjectView(R.id.layout_jbm)
    LinearLayout layoutJbm;

    @InjectView(layout_quxiaoqueren)
    LinearLayout layoutQuxiaoqueren;
    @InjectView(R.id.tetaborider)
    TextView tetaborider;
    @InjectView(R.id.querendigndan)
    Button querendigndan;
    @InjectView(R.id.zhongzhdigndan_static)
    Button zhongzhdigndanStatic;
    private File file = null;
    private AccmPlishActivitys accmPlishActivitys;
    private String dates;

    @Override
    public int setContentView() {
        return R.layout.lz_accomplish;
    }

    @Override
    public void initIalizeView() {
        ExitActivity.getInstance().addActivity(this);
        ButterKnife.inject(this);
        tetitle.setText("完成订单");
        accmPlishActivitys = new AccmPlishActivitys(this, this);
        accmPlishActivitys.setInit();
        if (getIntent().getStringExtra("gi_type").equals("j")) {
            layoutJbm.setVisibility(View.GONE);
            layoutWoaiwojia.setVisibility(View.VISIBLE);
            tetaborider.setText("我爱我家单");
        } else if (getIntent().getStringExtra("gi_type").equals("t")) {
            tetaborider.setText("家帮忙单");
            layoutQuxiaoqueren.setVisibility(View.GONE);
            layoutWoaiwojia.setVisibility(View.GONE);
        } else if (getIntent().getStringExtra("gi_type").equals("c")) {
            tetaborider.setText("公产房单");
        } else if (getIntent().getStringExtra("gi_type").equals("z")) {
            tetaborider.setText("公租房单");
        }
        Calendar now = Calendar.getInstance();
        String s=now.get(Calendar.YEAR)+"-";
        String q=(now.get(Calendar.MONTH) + 1)+"-";
        String w=now.get(Calendar.DAY_OF_MONTH)+"";
         dates=s+q+w;
        baoxiuqi.setText(dates);
//        tijiaoneirong.setVisibility(View.VISIBLE);
    }


    @OnClick({R.id.teleft, R.id.paizhao, R.id.tijiaoneirong, R.id.imdelete, R.id.baoxiuqi,R.id.querendigndan, R.id.zhongzhdigndan_static})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.teleft:
                finish();
                break;
            case R.id.paizhao:
                accmPlishActivitys.getPaizhao(1);
                break;
            case R.id.tijiaoneirong:
                    SubmitNeirong();

                break;
            case R.id.imdelete:
                paizhao.setImageDrawable(getResources().getDrawable(R.mipmap.paizhao));
                tjtp.setVisibility(View.VISIBLE);
                imdelete.setVisibility(View.GONE);
                accmPlishActivitys.getPaizhao(2);
                break;
            case R.id.baoxiuqi:
                Calendar c = Calendar.getInstance();
                new DatePickerDialog(this, 0, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        baoxiuqi.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
                    }
                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();

                break;
            case R.id.querendigndan:
                //false没有任何操作
                hints("您确定账期结算",handler,1);
                break;
            case R.id.zhongzhdigndan_static:
                //true线下支付功能
                hints("您确定现金结算",handler,2);
                break;
            default:
        }
    }
Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case 1:
                SubmitWoAiWoJia(false);
                break;
            case 2:
                SubmitWoAiWoJia(true);
                break;
            default:
        }
    }




    };

    private void SubmitWoAiWoJia(boolean pay) {
        if (fuwuneirong.getText().equals("")) {
            hint("服务内容不可以为空");
            return;
        } else if (fuwujine.getText().toString().equals("")) {
            hint("服务金额不可以为空");
            return;
        } else if (file == null) {
            hint("请拍摄维修单");
            return;
        } else {
            if (fuwujine.getText().toString().equals("")){
                fuwujine.setText("0");
            }
            if (cailiaojine.getText().toString().equals("")){
                cailiaojine.setText("0");
            }
            accmPlishActivitys.getSemdMessages(getIntent().getIntExtra("orid", 0), fuwuneirong.getText().toString(), cailiaomingxi.getText().toString(), fuwujine.getText().toString(), cailiaojine.getText().toString(), zongjine.getText().toString(),pay);
        }
    }


    private void SubmitNeirong() {
        String xudata = baoxiuqi.getText().toString();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
//        String data = df.format(new Date());// new Date()为获取当前系统时间
        Calendar now = Calendar.getInstance();
        String s=now.get(Calendar.YEAR)+"-";
        String q=(now.get(Calendar.MONTH) + 1)+"-";
        String w=now.get(Calendar.DAY_OF_MONTH)+"";
        String wdates=s+q+w;
//        StringBuffer star = new StringBuffer().append(wdates.substring(0, wdates.indexOf("-"))).append(wdates.substring(wdates.indexOf("-") + 1, wdates.lastIndexOf("-"))).append(wdates.substring(wdates.lastIndexOf("-") + 1));
//        StringBuffer end= new StringBuffer().append(xudata.substring(0, xudata.indexOf("-"))).append(xudata.substring(xudata.indexOf("-") + 1, xudata.lastIndexOf("-"))).append(xudata.substring(xudata.lastIndexOf("-") + 1));


        try {
//            if (Time_Transformation.stringToLong(wdates, Time_Transformation.StringG) - Time_Transformation.stringToLong(xudata, Time_Transformation.StringG) > 0) {
//                hint("请填写正确日期");
//                return;
//            } else
            if (Time_Transformation.stringToLong(xudata, Time_Transformation.StringG)-Time_Transformation.stringToLong(wdates, Time_Transformation.StringG)  < 0) {
                hint("请填写正确日期");
                return;
            } else
//            if (Time_Transformation.stringToLong(wdates, Time_Transformation.StringG) - Time_Transformation.stringToLong(xudata, Time_Transformation.StringG) > 0) {
//                hint("保修期不能为空");
//                return;
//            } else
// if (baoxiuqi.getText().toString().equals("")) {
//                hint("保修期不能为空");
//                return;
//            } else
 if (weixiuneirong.getText().toString().equals("")) {
                hint("报修内容不能为空");
                return;
            } else if (jine.getText().toString().equals("")) {
                hint("报修价格不能为空");
                return;
            } else if (weixiuneirong.getText().length() > 100) {
                hint("报修内容不能大于一百字");
                return;
            } else if (file == null) {
                hint("请拍摄维修单");
                return;
            } else {
                accmPlishActivitys.getSemdMessage();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        if (Integer.parseInt(star + "") > Integer.parseInt(end+"")) {
//            hint("请选择正确的保修期");
//            return;
//        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK)
            return;
        if (requestCode == 100) {
            file = (File) data.getSerializableExtra("file");
            accmPlishActivitys.getTuPianFile(file);//获取图片路径
            accmPlishActivitys.getTopMedia(file, getIntent().getIntExtra("orid", 0));//上传图片
            Toast.makeText(this, "请等待图片上传", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void setError(String name, DialogLoading dialogLoading) {
        if (dialogLoading != null) {
            dialogLoading.dismiss();
        }
        hint(name);
    }

    @Override
    public void setSucced(DialogLoading dialogLoading, int tag) {
        if (dialogLoading != null) {
            dialogLoading.dismiss();
        }
        Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
        if (tag == 1) {
            Intent inetnt;
            inetnt = new Intent("Isorder");
            inetnt.putExtra("name", "2");
            sendBroadcast(inetnt);
            finish();
        } else if (tag == 2) {
            if (getIntent().getStringExtra("gi_type").equals("j")) {
                layoutQuxiaoqueren.setVisibility(View.VISIBLE);
            } else if (getIntent().getStringExtra("gi_type").equals("t")) {
                tijiaoneirong.setVisibility(View.VISIBLE);
            }



        }
    }

    @Override
    public void setShowTupian(Intent intent) {//获取拍照和相册选择图片
        startActivityForResult(intent, 100);
    }

    @Override
    public void setUpdataTuPian(Bitmap bitmap) {//更新选择的图片
        paizhao.setImageBitmap(bitmap);
        tjtp.setVisibility(View.GONE);
        imdelete.setVisibility(View.VISIBLE);
    }

    @Override
    public String getGainBxq() {//获取保修期
        return baoxiuqi.getText().toString();
    }

    @Override
    public String getGainWxnr() {//获取维修内容
        return weixiuneirong.getText().toString();
    }

    @Override
    public String getGainWxjg() {//获取维修价格
        return jine.getText().toString();
    }

    @Override
    public int getGainOrid() {//获取微信订单
        return getIntent().getIntExtra("orid", 0);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
    }

}
